#include <windows.h>
#include "dbb.h"

//------------------------------------------------------------------------
inline
SimpleTimer::SimpleTimer(bool initAndRun)
:m_startTime(0)
,m_currentTime(0)
,m_stopTime(0)
{
	if(initAndRun)
	{
		m_startTime = GetTickCount();
	}
}

//------------------------------------------------------------------------
inline
SimpleTimer::~SimpleTimer()
{

}

//------------------------------------------------------------------------
inline void
SimpleTimer::start()
{
	dbbassert(!isRunning());
	QueryPerformanceCounter((LARGE_INTEGER*) &m_startTime);
	m_stopTime	= 0;
	m_currentTime	= m_startTime;
}

//------------------------------------------------------------------------
inline void
SimpleTimer::stop()
{
	dbbassert(isRunning());
	QueryPerformanceCounter((LARGE_INTEGER*) &m_stopTime);
}

//------------------------------------------------------------------------
inline void
SimpleTimer::reset()
{
	if (isRunning())
	{
		stop();
	}
	m_currentTime = 0;
}

//------------------------------------------------------------------------
inline bool
SimpleTimer::isRunning() const
{
	return 0!=m_startTime&&0==m_stopTime;
}

//------------------------------------------------------------------------
inline float
SimpleTimer::tickElapsed()
{
	dbbassert(isRunning());
	int64 current;
	QueryPerformanceCounter((LARGE_INTEGER*)&current);
	int64 freq;
	QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
	float elapsed = (float)(current-m_currentTime)/(float)(freq);
	m_currentTime = current;
	return elapsed;
}

//------------------------------------------------------------------------
inline float
SimpleTimer::runningTime() const
{
	int64 current;
	if(isRunning())
	{
		QueryPerformanceCounter((LARGE_INTEGER*)&current);
	}
	else
	{
		current = m_stopTime;
	}
	int64 freq;
	QueryPerformanceFrequency((LARGE_INTEGER*) &freq);
	return (float)(current-m_startTime)/(float)(freq); 
}

